package lerry.nape.factory;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import lerry.nape.factory.BodyFactory;
	
	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.shape.Shape;

	class GraphicFactory {
		/**
		 *圆 
		 * @param r半径
		 * @param color眼色
		 * @return DisplayObject
		 * 
		 */		
		public static function graphicCircle(r:Float, color:UInt):DisplayObject {
			var g:flash.display.Shape=new flash.display.Shape();
			g.graphics.beginFill(color);
			g.graphics.drawCircle(0, 0, r);
			return g;
		}
		/**
		 *矩形 
		 * @param width宽
		 * @param height高
		 * @param color颜色
		 * @return DisplayObject
		 * 
		 */		
		public static function graphicRec(width:Float, height:Float, color:UInt=0xff0000):DisplayObject {
			var g:flash.display.Shape=new flash.display.Shape();
			g.graphics.beginFill(color);
			g.graphics.drawRect(0, 0, width, height);
			return g;
		}
		/**
		 *根据body创建贴图 
		 * @param body目标body
		 * @param container容器
		 * @param fillColor填充颜色
		 * @param alpha透明度
		 * @return DisplayObject
		 * 
		 */
		public static function createGraphicForBody(body:Body, container:DisplayObjectContainer=null, fillColor:UInt=0xff0000, alpha:Float=1.0):DisplayObject {
			var shape:nape.shape.Shape=body.shapes.at(0);
			var g:Sprite=new Sprite();
			g.graphics.beginFill(fillColor, alpha);
			if (shape.isCircle()) {
				g.graphics.drawCircle(0, 0, shape.castCircle.radius);
				g.graphics.endFill();
				BodyFactory.setGraphic(body, g);
				return g;
			}
			for(i in 0...shape.castPolygon.localVerts.length){
				var v:Vec2=shape.castPolygon.localVerts.at(i);
				if (i == 0)
					g.graphics.moveTo(v.x, v.y);
				else
					g.graphics.lineTo(v.x, v.y);
			}
			g.graphics.endFill();
			BodyFactory.setGraphic(body, g,Vec2.get(),false,true);
			return g;
		}
		/**
		 *根据body创建位图 
		 * @param body目标
		 * @param container容器
		 * @param bitmap位图内容
		 * @param alpha透明度
		 * @return DisplayObject
		 * 
		 */		
		public static function createBitmapForBody(body:Body, container:DisplayObjectContainer=null, bitmap:Bitmap=null, alpha:Float=1.0):DisplayObject {
			var shape:nape.shape.Shape=body.shapes.at(0);
			
			var g:flash.display.Shape=new flash.display.Shape();
			g.graphics.beginBitmapFill(bitmap.bitmapData);
			if (shape.isCircle()) {
				g.graphics.drawCircle(0, 0, shape.castCircle.radius);
				g.graphics.endFill();
				BodyFactory.setGraphic(body, g);
				return g;
			}
			for(i in 0...shape.castPolygon.localVerts.length){
				var v:Vec2=shape.castPolygon.localVerts.at(i);
				if (i == 0)
					g.graphics.moveTo(v.x, v.y);
				else
					g.graphics.lineTo(v.x, v.y);
			}
			g.graphics.endFill();
			BodyFactory.setGraphic(body, g);
			return g;
		}
		/**
		 *为body创建骨骼贴图 
		 * @param body
		 * @param container
		 * 
		 */
		public static function createSkeletonForBody(body:Body, container:DisplayObjectContainer=null):Void {
			var p:Sprite=new Sprite();
			var g:flash.display.Shape=new flash.display.Shape();
			p.addChild(g);
			g.graphics.lineStyle(1, 0x00ff00);
			var shape:nape.shape.Shape=body.shapes.at(0);
			if (shape.isCircle()) {
				throw "body's shape must be reglur.";
				return;
			}
			for(i in 0...shape.castPolygon.localVerts.length){
				var v:Vec2=shape.castPolygon.localVerts.at(i);
				var s:flash.display.Shape=cast(graphicCircle(2,0xff0000),flash.display.Shape);
				s.x=v.x;
				s.y=v.y;
				if (i == 0)
					g.graphics.moveTo(v.x, v.y);
				else
					g.graphics.lineTo(v.x, v.y);
				p.addChild(s);
			}
			g.graphics.lineTo(shape.castPolygon.localVerts.at(0).x, shape.castPolygon.localVerts.at(0).y);
			BodyFactory.setGraphic(body, p);
			return;
		}
		/**
		 *为body创建矩形边框贴图 
		 * @param body目标
		 * @param container容器
		 * @param color颜色
		 * @param thickness粗细
		 * @param alpha透明度
		 * @return DisplayObject
		 * 
		 */
		public static function createRimForBody(body:Body,container:DisplayObjectContainer=null,color:UInt=0x000000,thickness:UInt=1,alpha:Float=1.0):DisplayObject{
			var shape:nape.shape.Shape=body.shapes.at(0);
			
			var g:flash.display.Shape=new flash.display.Shape();
			g.graphics.lineStyle(thickness,color,alpha);
			if (shape.isCircle()) {
				g.graphics.drawCircle(0, 0, shape.castCircle.radius);
				g.graphics.endFill();
				BodyFactory.setGraphic(body, g);
				return g;
			}
			for(i in 0...shape.castPolygon.localVerts.length){
				var v:Vec2=shape.castPolygon.localVerts.at(i);
				if (i == 0)
					g.graphics.moveTo(v.x, v.y);
				else
					g.graphics.lineTo(v.x, v.y);
			}
			g.graphics.lineTo(shape.castPolygon.localVerts.at(0).x,shape.castPolygon.localVerts.at(0).y);
			g.graphics.endFill();
			BodyFactory.setGraphic(body, g);
			return g;
		}
		/**
		 *显示网格 
		 * @param segmentsX x轴大小
		 * @param segmentsY y轴大小
		 * @param alpha 透明度
		 * 
		 */		
		public static function createGridding(container:DisplayObjectContainer,width:Float,height:Float,segmentsX:UInt=3,segmentsY:UInt=3,alpha:Float=.5):Void {
			var line:flash.display.Shape=new flash.display.Shape();
			line.graphics.lineStyle(1,0,alpha);
			container.addChild(line);
			var xstep:Float=width / segmentsX;
			var ystep:Float=height / segmentsY;
			for(i in 0...segmentsX){
				line.graphics.moveTo(xstep * i,0);
				line.graphics.lineTo(xstep * i,height);
			}
			for(j in 0...segmentsY){
				line.graphics.moveTo(0,ystep * j);
				line.graphics.lineTo(width,ystep * j);
			}
		}
	}

